package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.VMConst;
import com.vm.db.ShoppingCartDao;
import com.vm.model.ShoppingCart;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class ShoppingCartActivity extends Activity {
	ImageView imgbtn_search_back;
	TextView tv_all_number;
	ShoppingCart shoppingCart;
	ShoppingCartDao sDao;
	private List<ShoppingCart> list;
	ListView listView;
	LinearLayout found_ll_1;
	Float totalprice = (float) 0;
	TextView tv_money;
	List<String> goodsId = new ArrayList<String>();
	Button btn_delete;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping_cart);
		sDao = new ShoppingCartDao(ShoppingCartActivity.this);
		init();
		getTotalPrice();
	}

	public void init() {
		btn_delete = (Button) findViewById(R.id.btn_delete);
		btn_delete.setOnClickListener(delete_listener);
		tv_money = (TextView) findViewById(R.id.tv_money);
		found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		tv_all_number = (TextView) findViewById(R.id.tv_all_number);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		list = sDao.selectall();
		tv_all_number.setText(Integer.toString(list.size()));
		if (list.size() > 0 || list != null) {
			int i = 0;
			for (ShoppingCart shoppingCart1 : list) {
				shoppingCart = new ShoppingCart();
				shoppingCart.setGoodsId(shoppingCart1.getGoodsId());
				shoppingCart.setImageUrl(shoppingCart1.getImageUrl());
				shoppingCart.setName(shoppingCart1.getName());
				shoppingCart.setNumber(shoppingCart1.getNumber());
				shoppingCart.setPrice(shoppingCart1.getPrice());
				found_ll_1.addView(dyAddView(i, shoppingCart, 1));
			}
		}
	}

	OnClickListener delete_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (int i = 0; i < goodsId.size(); i++) {
				String id = goodsId.get(i);
				sDao.delete(id);
				Intent intent = new Intent(ShoppingCartActivity.this,
						ShoppingCartActivity.class);
				startActivity(intent);
				finish();
			}
		}

	};
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			onBackPressed();

		}
	};

	/**
	 * 返回按钮监听(手机自带返回按钮)
	 */
	@Override
	public void onBackPressed() {
		// Log.i(TAG, "返回按钮监听(手机自带返回按钮)");
		super.onBackPressed();
		// System.out.println("按下了back键   onBackPressed()");
	}

	public LinearLayout dyAddView(int i, final ShoppingCart shoppingCart,
			int progress) {
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.shopping_cart_item, null);
		ImageView iv_goodsImage = (ImageView) linearLayout
				.findViewById(R.id.iv_goodsImage);
		TextView tv_goodsName = (TextView) linearLayout
				.findViewById(R.id.tv_goodsName);
		final TextView tv_num = (TextView) linearLayout
				.findViewById(R.id.tv_num);
		final TextView tv_money = (TextView) linearLayout
				.findViewById(R.id.tv_money);
		ImageView imgbtn_add = (ImageView) linearLayout
				.findViewById(R.id.imgbtn_add);
		ImageView imgbtn_sub = (ImageView) linearLayout
				.findViewById(R.id.imgbtn_sub);
		CheckBox checkbox_set = (CheckBox) linearLayout
				.findViewById(R.id.checkbox_set);
		checkbox_set.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					totalprice += shoppingCart.getPrice()
							* Integer.parseInt(tv_num.getText().toString());
					getTotalPrice();
					goodsId.add(shoppingCart.getGoodsId());
					Log.e("list", Integer.toString(goodsId.size()));
				} else {
					totalprice -= shoppingCart.getPrice()
							* Integer.parseInt(tv_num.getText().toString());
					getTotalPrice();
					goodsId.remove(shoppingCart.getGoodsId());
					Log.e("list", Integer.toString(goodsId.size()));
				}
			}
		});
		if (checkbox_set.isChecked()) {
			totalprice += shoppingCart.getPrice();
			getTotalPrice();
			goodsId.add(shoppingCart.getGoodsId());
			Log.e("list", Integer.toString(goodsId.size()));
		}
		if (shoppingCart.getGoodsId() != null) {
			tv_goodsName.setTag(shoppingCart.getGoodsId());
		}
		if (shoppingCart.getName() != null) {
			tv_goodsName.setText(shoppingCart.getName());
		}
		tv_num.setText(Integer.toString(1));
		if (shoppingCart.getPrice() != null) {
			tv_money.setText(Float.toString(shoppingCart.getPrice()
					* Integer.parseInt(tv_num.getText().toString())));
		}
		ImageLoader.getInstance().displayImage(
				VMConst.URL + shoppingCart.getImageUrl(), iv_goodsImage,
				MyDisplayImageOptions.getInstance());
		imgbtn_add.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				int number = Integer.parseInt(tv_num.getText().toString());
				tv_num.setText(Integer.toString(number + 1));
				tv_money.setText(Float.toString(shoppingCart.getPrice()
						* Integer.parseInt(tv_num.getText().toString())));
				totalprice += shoppingCart.getPrice();
				getTotalPrice();
			}
		});
		imgbtn_sub.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				int number = Integer.parseInt(tv_num.getText().toString());
				if (number > 1) {
					tv_num.setText(Integer.toString(number - 1));
					tv_money.setText(Float.toString(shoppingCart.getPrice()
							* Integer.parseInt(tv_num.getText().toString())));
					totalprice -= shoppingCart.getPrice();
					getTotalPrice();
				}
			}
		});
		return linearLayout;
	}

	public void getTotalPrice() {
		tv_money.setText(Float.toString(totalprice));
	}
}